package com.typhoon.extend.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.typhoon.extend.dao.CountAndResult;
import com.typhoon.extend.dao.RoleDao;
import com.typhoon.extend.dto.RoleDto;
import com.typhoon.extend.dto.request.QueryRoleReq;
import com.typhoon.extend.entity.Role;
import com.typhoon.extend.service.RoleService;
import com.typhoon.extend.util.CollectionUtils;
import com.typhoon.extend.util.PageHolder;

@Service("roleService")
public class RoleServiceImpl implements RoleService {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(RoleServiceImpl.class);
	
	@Resource
	RoleDao roleDao;

	@Override
	public PageHolder<RoleDto> queryPageByCondition(QueryRoleReq req,int pageSize,int pageIndex) {
		if(null == req) {
			return null;
		}
		int offset = pageSize * (pageIndex - 1);
		CountAndResult<Role> result = this.roleDao.queryPageByCondition(req,offset,pageSize);
		if(null == result) {
			return null;
		}
		PageHolder<RoleDto> page = new PageHolder<>(pageSize);
		page.setRowCount(result.getCount());
		page.setList(CollectionUtils.transform(result.getList(), RoleDto.class));
		return page;
	}

	@Override
	public List<RoleDto> listAllByUserId(Long userId) {
		if(null == userId || userId <= 0) {
			throw new IllegalArgumentException("params illegal");
		}
		List<Role> list = this.roleDao.listAllByUserId(userId);
		if(null == list || list.isEmpty()) {
			return null;
		}
		return CollectionUtils.transform(list, RoleDto.class);
	}

}
